package com.mobi.cacti.ns.view;

import java.util.ArrayList;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.widget.ListView;

import com.mobi.cacti.R;
import com.mobi.cacti.ns.model.ListItem;
import com.mobi.cacti.ns.view.adapter.ItemListAdapter;
import com.mobi.cacti.ns.view.tab.BaseContainerFragment;
import com.mobi.cacti.rabbit.controllers.TicketCountActivityController;
import com.mobi.cacti.rabbit.controllers.TicketCountActivityControllerCallback;
import com.mobi.cacti.rabbit.response.Cache;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class CustInfoActivity extends Fragment implements TicketCountActivityControllerCallback{

	private TicketCountActivityController ticketCountActivityController;
	
	private ArrayList<ListItem> items;
	private ItemListAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		ticketCountActivityController = TicketCountActivityController.getInstance();
		ticketCountActivityController.delegate = this;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.custinfo_layout, null);
				
		items = new ArrayList<ListItem>();
		
		ListItem item = new ListItem();
		item.label = "Customer Service Info";
		item.ticketCountLabel = null;
		item.isShowRightArrow = true;
		items.add(item);
		
		item = new ListItem();
		item.label = "Ticket";
		item.ticketCountLabel = null;
		item.isShowRightArrow = true;
		items.add(item);
		
		item = new ListItem();
		item.label = "Contact Person";
		item.ticketCountLabel = null;
		item.isShowRightArrow = true;
		items.add(item);
		
		adapter = new ItemListAdapter(getSupportActivity(), R.layout.rowitem_layout, items);
		ListView listView = (ListView) v.findViewById(R.id.custinfolist);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				if (position == 0) {
					// Customer Service Info
					CustServiceInfoActivity custServiceInfoActivity = new CustServiceInfoActivity();
					
					((BaseContainerFragment)getParentFragment()).replaceFragment(custServiceInfoActivity, true);
				} else if (position == 1) {
					// Ticket
					TicketCircuitListActivity ticketCircuitListActivity = new TicketCircuitListActivity();
					
					((BaseContainerFragment)getParentFragment()).replaceFragment(ticketCircuitListActivity, true);
				} else if (position == 2) {
					// Contact Person
					ContactPersonActivity contactPersonActivity = new ContactPersonActivity();
					
					((BaseContainerFragment)getParentFragment()).replaceFragment(contactPersonActivity, true);
				}
			}
		});
		
		ticketCountActivityController.loadTicketCount(this.getActivity());
		
		return v;
	}

	@Override
	public void ticketCountActivityControllerCompleteCountTicket() {
		ListItem item = items.get(1);
		item.ticketCountLabel = (Cache.getTotlaTicketCount() == 0) ? null : Cache.getTotlaTicketCount() + "";
		adapter.notifyDataSetChanged();
	}
}
